package com.hnyyjsxy.web.servlet;

import com.hnyyjsxy.domain.AccountSheet;
import com.hnyyjsxy.domain.ResultInfo;
import com.hnyyjsxy.domain.Student;
import com.hnyyjsxy.service.UserService;
import com.hnyyjsxy.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @date 2021/02/04
 */
@WebServlet("/user/*")
public class UserServlet extends BeanServlet {
    //创建UserService对象
    UserService userService = new UserServiceImpl();

    /**
     * 用于用户的注册
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 使用BeanUtils获取请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        Student student = new Student();
        try {
            BeanUtils.populate(student,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        //创建一个ResultInfo对象
        ResultInfo resultInfo = new ResultInfo();

        //2. 判断两次输入密码是否相同
        String password = request.getParameter("password");
        String password1 = request.getParameter("password1");
        if (!(password!=null && password.equals(password1))){
            //两次密码不相同
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("两次输入密码不一致");
        }else {
            //密码相同
            //3. 判断验证码是否正确
            String check = request.getParameter("check");
            String checkcode = (String) request.getSession().getAttribute("CHECKCODE_SERVER");
            if (!checkcode.equalsIgnoreCase(check)){
                //验证码不正确
                resultInfo.setFlag(false);
                resultInfo.setErrorMsg("验证码不正确");
            }else{
                //验证码正确
                //4. 判断该学号是否存在
                boolean bySid = userService.findBySid(student.getSid());
                if (bySid){
                    //该学号存在
                    resultInfo.setFlag(false);
                    resultInfo.setErrorMsg("该学号已存在");
                }else{
                    //5. 其他条件都满足 可以注册 进行数据库写入
                    userService.register(student);
                    userService.addAccount(student.getSid(),password);
                    resultInfo.setFlag(true);
                }
            }
        }

        //7. 响应,将resultInfo转json并回响给前端
        respondOnly(resultInfo,response);
    }

    /**
     * 用户登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String check = request.getParameter("check");
        //2. 创建ResultInfo对象
        ResultInfo resultInfo = new ResultInfo();
        //3. 获取session中的验证码
        HttpSession session = request.getSession();
        String checkcode = (String) session.getAttribute("CHECKCODE_SERVER");
        //删除验证码，避免一个验证码重复使用
        //session.removeAttribute("CHECKCODE_SERVER");
        //4. 判断验证码是否正确
        if (!checkcode.equalsIgnoreCase(check)){
            //验证码不正确
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("验证码不正确");
        }else{
            //5. 验证码正确，判断用户名密码是否正确
            AccountSheet accountSheet = userService.userlogin(username, password);
            if (accountSheet == null){
                //用户名密码不正确
                resultInfo.setFlag(false);
                resultInfo.setErrorMsg("用户名或密码不正确");
            }else{
                //用户名密码正确
                resultInfo.setFlag(true);
                resultInfo.setData(accountSheet);
                //登录成功，将用户账号信息存入session域中
                session.setAttribute("user",accountSheet);
            }
        }
        //6. 响应,将resultInfo转json并回响给前端
        respondOnly(resultInfo,response);
    }
}
